﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Lswweb.Domain;
using Lswweb.Model;

namespace Lswweb.Controls
{
    public class TreeIconCreate : SkinnedWebControl
    {
        #region 成员字段和构造函数

        private TreeViewDropDownList ddlTree;
        private ImageUpload fileUpload;
        private Button btnAdd;

        #endregion

        #region 初始化皮服

        protected override void InitializeSkin(System.Web.UI.Control skin)
        {
            ddlTree = (TreeViewDropDownList)skin.FindControl("ddlTree");
            fileUpload = (ImageUpload)skin.FindControl("fileUpload");
            btnAdd = (Button)skin.FindControl("btnAdd");

            fileUpload.AutoCreateFileName = true;
            ddlTree.ParentId = this.ParentId;
            ddlTree.Type = this.TreeType;

            btnAdd.Click+=new EventHandler(btnAdd_Click);
        }

        void btnAdd_Click(object sender, EventArgs e)
        {
            if (fileUpload.HasFile)
            {
                string url = fileUpload.SaveAs();
                if (!string.IsNullOrEmpty(url))
                    TreeTables.TreeTable(TreeType).UpdateIcon(Id,url);
            }
            else
            {
                SiteContext.RenderMessage(ExceptionType.FileNotFound);

            }
        }

        #endregion

        private int Id
        {
            get { return Convert.ToInt32(ddlTree.SelectedValue); }
        }

        private int parentId = SiteContext.Current.CategoryId;
        public int ParentId
        {
            get { return this.parentId == -1 ? 0 : this.parentId; }
            set { parentId = value; }
        }


        private TreeTableType _type = (TreeTableType)SiteContext.Current.Type;
        public TreeTableType TreeType
        {
            get { return this._type; }
            set { this._type = value; }
        }

        protected override string DefaultSkinFilename
        {
            get { return "TreeView/TreeIconCreate.ascx"; }
        }
    }
}
